const breadcrumbsHandler = {
	createBreadCrumbs(menuArray) {
		let breadcrumbsObject = {};
		menuArray.forEach(menu => {
			if (menu.path) {
				this.breadcrumbItemsAdd(menu, null, null, breadcrumbsObject);
			} else if (menu.children && menu.children.length > 0) {
				menu.children.forEach(child => {
					if (child.path) {
						this.breadcrumbItemsAdd(child, menu, null, breadcrumbsObject);
					} else if (child.children && child.children.length > 0) {
						child.children.forEach(item => {
							if (item.path) {
								this.breadcrumbItemsAdd(item, menu, child, breadcrumbsObject);
							}
						});
					}
				});
			}
		});
		return breadcrumbsObject;
	},

	breadcrumbItemsAdd(third, first, second, breadcrumbsObject) {
		let breadcrumb = {};
		let breadcrumbItems = [];
		if (first) {
			breadcrumb = {
				name: first.name,
				title: first.title,
				icon: first.icon
			};
			breadcrumbItems.push(breadcrumb);
		}
		if (second) {
			breadcrumb = {
				name: second.name,
				title: second.title,
				icon: second.icon
			};
			breadcrumbItems.push(breadcrumb);
		}
		breadcrumb = {
			name: third.name,
			title: third.title,
			icon: third.icon,
			path: third.path
		};
		breadcrumbItems.push(breadcrumb);
		breadcrumbsObject[third.path] = breadcrumbItems;
	}
};

export default breadcrumbsHandler;
